package com.breeze.components.oss.mybaits.entity;

import com.alibaba.fastjson2.JSON;
import com.breeze.components.core.util.DateUtil;
import com.breeze.components.mybatis.base.BaseEntity;
import com.mybatisflex.annotation.Table;
import io.swagger.v3.oas.annotations.media.Schema;
import java.io.Serial;
import java.io.Serializable;
import java.util.Objects;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.dromara.x.file.storage.core.upload.FilePartInfo;

/**
 * 文件分片信息表，仅在手动分片上传时使用 实体类。
 *
 * @author breeze
 * @since 2024-05-13 10:08:23
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@Schema(title = "文件分片信息表，仅在手动分片上传时使用")
@Table("sys_file_part_detail")
public class FilePartDetailDo extends BaseEntity implements Serializable {

  @Serial private static final long serialVersionUID = 1L;

  /** 存储平台 */
  @Schema(title = "存储平台")
  private String platform;

  /** 上传ID，仅在手动分片上传时使用 */
  @Schema(title = "上传ID，仅在手动分片上传时使用")
  private String uploadId;

  /** 分片 ETag */
  @Schema(title = "分片 ETag")
  private String eTag;

  /** 分片号。每一个上传的分片都有一个分片号，一般情况下取值范围是1~10000 */
  @Schema(title = "分片号。每一个上传的分片都有一个分片号，一般情况下取值范围是1~10000")
  private Integer partNumber;

  /** 文件大小，单位字节 */
  @Schema(title = "文件大小，单位字节")
  private Long partSize;

  /** 哈希信息 */
  @Schema(title = "哈希信息")
  private String hashInfo;

  /**
   * 将 FilePartInfo 转成 FilePartDetail
   *
   * @param info 文件分片信息
   */
  public static FilePartDetailDo toFilePartDetail(FilePartInfo info) {
    if (Objects.isNull(info)) {
      return null;
    }
    var detail = new FilePartDetailDo();
    detail.setPlatform(info.getPlatform());
    detail.setUploadId(info.getUploadId());
    detail.setETag(info.getETag());
    detail.setPartNumber(info.getPartNumber());
    detail.setPartSize(info.getPartSize());
    detail.setHashInfo(JSON.toJSONString(info.getHashInfo()));
    detail.setCreateTime(DateUtil.toLocalDateTime(info.getCreateTime()));
    return detail;
  }
}
